package com.atguigu.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atguigu.bean.User;
import com.atguigu.service.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping("/register")
	@ResponseBody
	public Map registerController(@RequestParam("account")String account,
			@RequestParam("password")String password){
		Map<String,String> map = new HashMap<>();
		User registerUser = new User(null,account,password);
		int i = userService.insertUser(registerUser);
		if (i>0) {
			System.out.println(registerUser);
			map.put("msg", "ok");
		} else {
			System.out.println(registerUser);
			map.put("msg", "err");
		}
		return map;
	}
	
	@RequestMapping("/login")
	@ResponseBody
	public Map loginController(@RequestParam("account")String account,@RequestParam("password")String password){
		Map<String,String> map = new HashMap<>();
		User registerUser = userService.getUserByAccountAndPassword(account, password);
		User loginUser = new User(null,account,password);
		
		if (loginUser.getAccount().equals(registerUser.getAccount()) && loginUser.getPassword().equals(registerUser.getPassword())) {
			System.out.println("loginUser"+loginUser);
			map.put("message", "ok");
		} else {
			System.out.println("loginUser"+loginUser);
			map.put("message", "err");
		}
		return map;
	}
}
